use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    /// 生成配置
    #[clap(visible_aliases = & ["gcfg"])]
    GenConfig(GenConfig),
    #[clap(visible_aliases = & ["t"])]
    Test,
}

#[derive(Parser, Debug)]
pub struct GenConfig {
    /// 配置文件路径
    #[arg(short, long, value_parser = parse_path)]
    pub config_path: String,
    ///配置输出文件名
    #[arg(short, long)]
    pub name: String,
}

/// 解析路径是否合法or是否存在
fn parse_path(s: &str) -> Result<String, String> {
    let path = std::path::Path::new(s);
    if !path.exists() {
        return Err(format!("路径不存在:{}", s));
    }
    //判断结尾是否有\ 如果有则去掉
    if s.ends_with('\\') || s.ends_with('/') {
        return Ok(s[0..s.len() - 1].to_string());
    }
    Ok(s.to_string())
}
